/**
 * @Author: Jay
 * @Date: 2023/7/31 16:08
 */

import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.fastjson.JSONObject;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;

import java.io.IOException;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * @program: skuCalcDate
 * @description:
 * @author: Jay
 * @create: 2023-07-31 16:08
 **/

public class SkuInfoDataService {


    public static void main(String[] args) throws IOException, ParseException {
        List<SkuCalcDate> dateList = ExportExcelUtil.readSkuInfo();
        HashMap<String, SkuOnTheWayInfo> dataEast = ExportExcelUtil.readSkuOnTheWayEast();
        HashMap<String, SkuOnTheWayInfo> dataWest = ExportExcelUtil.readSkuOnTheWayWest();
        calcDateAndNum(dateList, dataEast, dataWest);
    }

    private static void calcDateAndNum(List<SkuCalcDate> dateList, HashMap<String, SkuOnTheWayInfo> dataEast, HashMap<String, SkuOnTheWayInfo> dataWest) throws ParseException {

        for (SkuCalcDate skuCalcDate : dateList) {
            calcData(skuCalcDate, dataEast, AreaEnum.East);
            calcData(skuCalcDate, dataWest, AreaEnum.Mid);
            //calcData(skuCalcDate, dataEast);
            calcData(skuCalcDate, dataWest, AreaEnum.West);
            //skuCalcDate.getRevolveDayEast()
        }

        ExportExcelUtil.writeWithTemplate("E:\\github_java\\skuCalcDate\\src\\main\\resources\\skuNew.xlsx", dateList);
    }

    private static void calcData(SkuCalcDate skuCalcDate, HashMap<String, SkuOnTheWayInfo> data, AreaEnum areaEnum) throws ParseException {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        DateTimeFormatter sp = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        //1.先取到当前在哪个区间,第三个销量应该是从7月13日开始计算假设为18件每天，46/18=2.66天，那就7月15日售罄。
        //后续途中货物B7月18日到货100件，100/18=5.55天,就是7月23号售罄，
        //1.东部库存+东部在途=东部总库存
        //2.扣欠5列每日销量，假设最后一列是8.3-至今 ，算出(今天-8.3)的销量，得出最后剩余销量
        //3.用剩余销量/最后一列每日销量=得出剩下可卖天数
        //4.今天+得出剩下可卖天数=售罄日期 及可卖数量及体积立方米
        Double allNumber = 0D;
        if (AreaEnum.East == areaEnum) {
            Double sumStockEast = 0D;
            if (data.get(skuCalcDate.getSku()) != null) {
                sumStockEast = data.get(skuCalcDate.getSku()).getSumStockEast();
            }
            allNumber = getNull(skuCalcDate.getStockEast()) + sumStockEast;
        } else if (AreaEnum.Mid == areaEnum) {
            Double sumStockWest = 0D;
            if (data.get(skuCalcDate.getSku()) != null) {
                sumStockWest = data.get(skuCalcDate.getSku()).getSumStockWest();
            }
            allNumber = getNull(skuCalcDate.getStockWest()) + sumStockWest;
        } else if (AreaEnum.West == areaEnum) {
            Double sumStockWest = 0D;
            if (data.get(skuCalcDate.getSku()) != null) {
                sumStockWest = data.get(skuCalcDate.getSku()).getSumStockWest();
            }
            allNumber = getNull(skuCalcDate.getStockWest()) + sumStockWest;
        }

        //7.12-7.18
        Double numberOne = daysNumber(skuCalcDate.getAvgSaleDayOneDate(), skuCalcDate.getAvgSaleDayOne());
        Double numberTwo = daysNumber(skuCalcDate.getAvgSaleDayTwoDate(), skuCalcDate.getAvgSaleDayTwo());
        Double numberThree = daysNumber(skuCalcDate.getAvgSaleDayThreeDate(), skuCalcDate.getAvgSaleDayThree());
        Double numberFour = daysNumber(skuCalcDate.getAvgSaleDayFourDate(), skuCalcDate.getAvgSaleDayFour());
        //到今天的销售数量
        Double numberFive = daysNumber(skuCalcDate.getAvgSaleDayFiveDate(), skuCalcDate.getAvgSaleDayFive());
        Double remainingDays;
        if (skuCalcDate.getAvgSaleDayFive() > 0) {
            remainingDays = (allNumber - numberOne - numberTwo - numberThree - numberFour - numberFive) / skuCalcDate.getAvgSaleDayFive();
        } else {
            remainingDays = 0D;
        }
        //剩下可卖天数
        LocalDateTime localDateTime = LocalDateTime.now().plusDays(remainingDays.longValue());
        //需要补货（30天-（7月23日-7月21号+0.55）=27.45天，18件每天*27.45天=494.1件  去零头就是494件
        //7月23日是前面计算出来库存和所有途中的最后的售罄日期
        //7月21日是我计划补货到货的日期  需要我自己填写  这个表格里没写到这个
        //补货到货的日期
        if (AreaEnum.East == areaEnum) {
            LocalDateTime localDateTime2 = LocalDateTime.parse(skuCalcDate.getDateEast(), formatter).plusDays(remainingDays.longValue());
            long arriveDateEast = skuCalcDate.getRevolveDayEast().longValue() - (getDays(Timestamp.valueOf(localDateTime2).getTime(), Timestamp.valueOf(localDateTime).getTime()));
            //天数*件数
            long sumEast = checkData(arriveDateEast * skuCalcDate.getAvgSaleDayFive().longValue());
            //体积
            long volumeEast = skuCalcDate.getLength().longValue() * skuCalcDate.getWidth().longValue() * skuCalcDate.getHeight().longValue() / 1000000 * sumEast;
            skuCalcDate.setArriveDateEast(LocalDateTime.now().plusDays(arriveDateEast).format(formatter));
            skuCalcDate.setSumEast(sumEast);
            skuCalcDate.setVolumeEast(volumeEast);
        }

        if (AreaEnum.Mid == areaEnum) {
            LocalDateTime localDateTime2 = LocalDateTime.parse(skuCalcDate.getDateMid(), formatter).plusDays(remainingDays.longValue());
            long arriveDateMid = skuCalcDate.getRevolveDayMid().longValue() - (getDays(Timestamp.valueOf(localDateTime2).getTime(), Timestamp.valueOf(localDateTime).getTime()));
            //天数*件数
            long sumMid = checkData(arriveDateMid * skuCalcDate.getAvgSaleDayFive().longValue());
            //体积
            long volumeMid = skuCalcDate.getLength().longValue() * skuCalcDate.getWidth().longValue() * skuCalcDate.getHeight().longValue() / 1000000 * sumMid;
            skuCalcDate.setArriveDateMid(LocalDateTime.now().plusDays(arriveDateMid).format(formatter));
            skuCalcDate.setSumMid(sumMid);
            skuCalcDate.setVolumeMid(volumeMid);
        }

        if (AreaEnum.West == areaEnum) {
            LocalDateTime localDateTime2 = LocalDateTime.parse(skuCalcDate.getDateWest(), formatter).plusDays(remainingDays.longValue());
            long arriveDateWest = skuCalcDate.getRevolveDayWest().longValue() - (getDays(Timestamp.valueOf(localDateTime2).getTime(), Timestamp.valueOf(localDateTime).getTime()));
            //天数*件数
            long sumWest = checkData(arriveDateWest * skuCalcDate.getAvgSaleDayFive().longValue());
            //体积
            long volumeWest = skuCalcDate.getLength().longValue() * skuCalcDate.getWidth().longValue() * skuCalcDate.getHeight().longValue() / 1000000 * sumWest;
            skuCalcDate.setArriveDateWest(LocalDateTime.now().plusDays(arriveDateWest).format(formatter));
            skuCalcDate.setSumWest(sumWest);
            skuCalcDate.setVolumeWest(volumeWest);
        }
        System.out.println(JSONObject.toJSONString(skuCalcDate));
    }

    private static long checkData(long num) {
        return num < 0 ? 0 : num;
    }

    private static Double getNull(Double price) {
        return price == null ? 0 : price;
    }

    private static Double daysNumber(String avgSaleDayOneDate, Double avgSaleDayOne) throws ParseException {
        DateFormat dd = new SimpleDateFormat("yyyy/MM/dd");
        String[] date = avgSaleDayOneDate.split("-");
        Date startBegin;
        Date endBegin;
        if (date.length > 1) {
            startBegin = dd.parse(date[0]);
            endBegin = dd.parse(date[1]);
            //相出天数
            return getDays(startBegin, endBegin) * avgSaleDayOne;
        } else {
            //至今的
            startBegin = dd.parse(date[0]);
            endBegin = new Date();
            return getDays(startBegin, endBegin) * avgSaleDayOne;
        }
    }

    private static long getDays(Date startBegin, Date endBegin) {
        //返回天数
        return (endBegin.getTime() - startBegin.getTime()) / (24 * 3600 * 1000);
    }

    private static long getDays(long startBegin, long endBegin) {
        //返回天数
        return (endBegin - startBegin) / (24 * 3600 * 1000);
    }
}